package com.campus.common.bean.common;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@io.swagger.annotations.ApiModel(value = "统一响应体", description = "系统统一响应格式")
public class Resp<T> {
    public static final String CODE_ERROR = "1";
    public static final String CODE_SUCCESS = "0";

    public static final String MESSAGE_SUCCESS = "操作成功";


    @ApiModelProperty(value = "响应状态码", notes = "0:成功, 1:失败, 其他:自定义错误码", required = true, dataType = "string", position = 0, example = "0")
    private String code;
    @ApiModelProperty(value = "响应消息", notes = "成功或失败的描述信息", required = true, dataType = "string", position = 1, example = "操作成功")
    private String message;
    @ApiModelProperty(value = "响应数据", notes = "返回的业务数据", required = false, position = 2)
    private T data;
    
    public Resp() {
    	super();
    }

    @SuppressWarnings("unchecked")
	private Resp(String code, String message, T data) {
    	super();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Resp<T> success() {
        return new Resp<T>(CODE_SUCCESS, MESSAGE_SUCCESS, null);
    }

    public static <T> Resp<T> success(T object) {
        return new Resp<>(CODE_SUCCESS, MESSAGE_SUCCESS, object);
    }

    public static <T> Resp<T> error(String message) {
        return new Resp<T>(CODE_ERROR, message, null);
    }

    public static <T> Resp<T> error(String code, String message) {
        return new Resp<T>(code, message, null);
    }

    public static <T> Resp<T> error(String code, String message, T object) {
        return new Resp<>(code, message, object);
    }


    public static <T> Resp<Pagination<T>> successPageable(PageInfo<T> pageInfo) {
        Pagination<T> pagination = new Pagination<>();
        pagination.setCurrentPage(pageInfo.getPageNum());
        pagination.setPageSize(pageInfo.getPageSize());
        pagination.setTotal((int) pageInfo.getTotal());
        pagination.setList(pageInfo.getList());
        return new Resp<Pagination<T>>(CODE_SUCCESS, MESSAGE_SUCCESS, pagination);
    }

    public static <D, S extends D> Resp<Pagination<D>> successPageable(PageInfo<S> pageInfo, Class<D> clazz) {
        Pagination<D> pagination = new Pagination<>();
        pagination.setCurrentPage(pageInfo.getPageNum());
        pagination.setPageSize(pageInfo.getPageSize());
        pagination.setTotal((int)pageInfo.getTotal());
        List<D> list = new ArrayList<>();
        list.addAll(pageInfo.getList());
        pagination.setList(list);
        return new Resp<Pagination<D>>(CODE_SUCCESS, MESSAGE_SUCCESS, pagination);
    }


}